package com.anhry.app.safety.util;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * @date:2011-10-12
 * @remark:
 * @author:dongbin
 */
public class DateUtil {

	/** 
	 * 取得当前日期是多少周 
	 * 
	 * @param date 
	 * @return 
	 */ 
	 public static int getWeekOfYear(Date date) { 
	 Calendar c = new GregorianCalendar(); 
	 c.setFirstDayOfWeek(Calendar.MONDAY); 
	 c.setMinimalDaysInFirstWeek(7); 
	 c.setTime (date);

	 return c.get(Calendar.WEEK_OF_YEAR); 
	 }

	 /** 
	 * 得到某一年周的总数 
	 * 
	 * @param year 
	 * @return 
	 */ 
	 public static int getMaxWeekNumOfYear(int year) { 
	 Calendar c = new GregorianCalendar(); 
	 c.set(year, Calendar.DECEMBER, 31, 23, 59, 59);

	 return getWeekOfYear(c.getTime()); 
	 }

	 /** 
	 * 得到某年某周的第一天 
	 * 
	 * @param year 
	 * @param week 
	 * @return 
	 */ 
	 public static Date getFirstDayOfWeek(int year, int week) { 
	 Calendar c = new GregorianCalendar(); 
	 c.set(Calendar.YEAR, year); 
	 c.set (Calendar.MONTH, Calendar.JANUARY); 
	 c.set(Calendar.DATE, 1);

	 Calendar cal = (GregorianCalendar) c.clone(); 
	 cal.add(Calendar.DATE, week * 7);

	 return getFirstDayOfWeek(cal.getTime ()); 
	 }

	 /** 
	 * 得到某年某周的最后一天 
	 * 
	 * @param year 
	 * @param week 
	 * @return 
	 */ 
	 public static Date getLastDayOfWeek(int year, int week) { 
	 Calendar c = new GregorianCalendar(); 
	 c.set(Calendar.YEAR, year); 
	 c.set(Calendar.MONTH, Calendar.JANUARY); 
	 c.set(Calendar.DATE, 1);

	 Calendar cal = (GregorianCalendar) c.clone(); 
	 cal.add(Calendar.DATE , week * 7);

	 return getLastDayOfWeek(cal.getTime()); 
	 }

	 /** 
	 * 取得指定日期所在周的第一天 
	 * 
	 * @param date 
	 * @return 
	 */ 
	 public static Date getFirstDayOfWeek(Date date) { 
	 Calendar c = new GregorianCalendar(); 
	 c.setFirstDayOfWeek(Calendar.MONDAY); 
	 c.setTime(date); 
	 c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek()); // Monday 
	 return c.getTime (); 
	 }

	 /** 
	 * 取得指定日期所在周的最后一天 
	 * 
	 * @param date 
	 * @return 
	 */ 
	 public static Date getLastDayOfWeek(Date date) { 
	 Calendar c = new GregorianCalendar(); 
	 c.setFirstDayOfWeek(Calendar.MONDAY); 
	 c.setTime(date); 
	 c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek() + 6); // Sunday 
	 return c.getTime(); 
	 } 
	 
	 /** 
	 * 取得当前日期所在周的第一天 
	 * 
	 * @param date 
	 * @return 
	 */ 
	 public static Date getFirstDayOfWeek() { 
	 Calendar c = new GregorianCalendar(); 
	 c.setFirstDayOfWeek(Calendar.MONDAY); 
	 c.setTime(new Date()); 
	 c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek()); // Monday 
	 return c.getTime (); 
	 }

	 /** 
	 * 取得当前日期所在周的最后一天 
	 * 
	 * @param date 
	 * @return 
	 */ 
	 public static Date getLastDayOfWeek() { 
	 Calendar c = new GregorianCalendar(); 
	 c.setFirstDayOfWeek(Calendar.MONDAY); 
	 c.setTime(new Date()); 
	 c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek() + 6); // Sunday 
	 return c.getTime(); 
	 }  
	  
	 public static void main(String[] args) { 
	  int year = 2009; 
	  int week = 1;

	  // 以2006-01-02位例 
	  Calendar c = new GregorianCalendar(); 
	  c.set(2009, Calendar.DECEMBER, 7); 
	  Date d = c.getTime();

	  System.out.println("current date = " + d); 
	  System.out.println("getWeekOfYear = " + getWeekOfYear(d)); 
	  System.out.println("getMaxWeekNumOfYear = " + getMaxWeekNumOfYear(year)); 
	  System.out.println("getFirstDayOfWeek = " + getFirstDayOfWeek(year, week)); 
	  System.out.println("getLastDayOfWeek = " + getLastDayOfWeek(year, week)); 
	  System.out.println ("getFirstDayOfWeek = " + getFirstDayOfWeek(d)); 
	  System.out.println("getLastDayOfWeek = " + getLastDayOfWeek(d)); 
	  System.out.println ("getFirstDayOfWeek = " + getFirstDayOfWeek()); 
	  System.out.println("getLastDayOfWeek = " + getLastDayOfWeek()); 
	  }
	  
	 public static int getCurrentSeason() {
			int month = Calendar.getInstance().get(Calendar.MONTH);
			int day = Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
			int season = 0;
			if(day<8){//如果当前日小于8号 
				if(month==1){//如果是一月
					season = 4;
				} else if(month >=2 && month <5) {
					season = 1;
				} else if(month >=5 && month <8) {
					season = 2;
				} else if(month >=8 && month <11) {
					season = 3;
				} 
			}else{
				if(month >= 1 && month <4) {
					season = 1;
				} else if(month >=4 && month <7) {
					season = 2;
				} else if(month >=7 && month <10) {
					season = 3;
				} else if(month >=10 && month <12) {
					season = 4;
				} 
			}
			
			return season;
		}
	 
	 public static int  currentReportPeriod() {
			int whichHalf = 0;
			Calendar ca = Calendar.getInstance();
			int month = ca.get(Calendar.MONTH)+1;
			int day = ca.get(Calendar.DATE);
			Environment env = Environment.getInstance();
			//半月报从哪一天开始
			int startDayHalf = env.getIntProperty("corp.half.month.start");
			int middleDayHalf = env.getIntProperty("corp.half.month.middle");
			int endDayHalf = env.getIntProperty("corp.half.month.end");
			
			//半月报
				if(day >= startDayHalf && day<= middleDayHalf){//如果当前日小于中间日期  那么应该是上半月 
					//System.out.println(month+"月上半月");//当前自查月
					//System.out.println(month+"月" + startDayHalf + "日——"+month+"月" + middleDayHalf + "日");
				}else if( day >middleDayHalf ){ //下半月
					if(day <=endDayHalf) {
						//System.out.println(month+"月下半月");//当前自查月
						//System.out.println(month+"月" + (middleDayHalf + 1) + "日——"+month+"月" + endDayHalf + "日");
						whichHalf = 1;
					}  else {
						if(month==1){//如果是一月
							//System.out.println("12月下半月");
							//System.out.println("12月" + middleDayHalf + "日——1月" + endDayHalf + "日");
							whichHalf = 1;
						}else{
							//System.out.println((month-1)+"月下半月");
							//System.out.println((month-1)+"月" + middleDayHalf + "日——"+month+"月" + endDayHalf + "日");
							whichHalf = 1;
						}
					}
				} else if(day <= endDayHalf) {
					if(month==1){//如果是一月
						//System.out.println("12月下半月");
						//System.out.println("12月" + middleDayHalf + "日——1月" + endDayHalf + "日");
						whichHalf = 1;
					}else{
						whichHalf = 1;
						//System.out.println((month-1)+"月下半月");
						//System.out.println((month-1)+"月" + middleDayHalf + "日——"+month+"月" + endDayHalf + "日");
					}
				}
				
				return whichHalf;
		}
	}
